<?php

namespace App\Console\Commands;

use App\Imports\HospitalImport;
use App\Jobs\SyncDataChild;
use App\Jobs\TempPush;
use App\Models\FamilyEdu;
use App\Models\Hospital;
use App\Console\Commands\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use App\Models\FamilyType;
use App\Models\HospitalImportChild;
use App\Models\HospitalUploadData;
use App\Models\old\HospitalAppoint;
use App\Models\old\HospitalAppointStreet;
use App\Models\old\HospitalAppointVaccineTimeNum;
use App\Models\old\HospitalAppointWeek;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\Street;
use App\Models\TempPushRecord;
use App\Models\Vaccine;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Maatwebsite\Excel\Facades\Excel;
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use App\Exceptions\Arr;

class ComparisonV extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'comparisonv';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $hospitals = OldHospital::query()->get();
        foreach($hospitals as $hospital){
            $vaccinesOld = [];
            $vaccinesNew = [];
            $idsAll = [];
            echo $hospital->name;
            echo "\n";
            try {
            $response = Http::withOptions([
                'verify' => false,
            ])->timeout(100)->get('https://ebbapi.binglang6.com/new/ebb/hospital/getHospitalVaccineType/'.$hospital->userid);
                $data = $response->json();

                $list = $data['data']['childrenFreeVaccine'];
                $list = $list??[];
                $ids = Arr::pluck($list,'id');
                $idsAll = array_merge($idsAll,$ids);

                $list = $data['data']['childrenPayVaccine'];
                $list = $list??[];
                $ids = Arr::pluck($list,'id');
                $idsAll = array_merge($idsAll,$ids);

//                $list = $data['data']['adultVaccine'];
//                $list = $list??[];
//                $ids = Arr::pluck($list,'id');
//                $idsAll = array_merge($idsAll,$ids);
            }catch (\Exception $e){
                echo $e->getMessage();
            }
            try {
                foreach ($idsAll as $id) {
                    $responseN = Http::withOptions([
                        'verify' => false,
                    ])->timeout(100)->get('https://ebbapi.binglang6.com/new/ebb/hospital/getHospitalVaccine/' . $id . '/' . $hospital->userid);
                    $dataN = $responseN->json();
                    $vaccine = Arr::pluck($dataN['data'], 'id');
                    $vaccinesNew = array_merge($vaccinesNew, $vaccine);
                }
            }catch (\Exception $e){
                echo $e->getMessage();
            }

            foreach($vaccinesNew as $vac){
                $datas = [];
                $datasO = [];

                $response = Http::withOptions([
                    'verify' => false,
                ])
                    ->timeout(100)
                    ->post(
                        'https://ebbapi.binglang6.com/new/ebb/hospital/getAppointNum',
                        [
                            'appointType'=>2,
                            'project'=>$vac,
                            'hospitalId'=>$hospital->userid
                        ],
                    );
                $data = $response->json();
                if(isset($data['data'])){
                    $list = $data['data'];
                    $dataArr = array_filter($list, function ($where)  {
                        return $where['isAppoint'] == 1;
                    });
                    $data = Arr::pluck($dataArr,'date');
                    $datas = array_merge($datas,$data);
                }
                $vaccineView = Vaccine::query()->where('id',$vac)->first();
                echo $vaccineView->name;
                echo "\n";
                echo "new:\n";
                echo implode(',',Arr::sort($datas));
                echo "\n";
                $response = Http::withOptions([
                    'verify' => false,
                ])->get('https://api.child.wedoctors.com.cn/v4/appoint/form?id='.$hospital->userid."&type=2"."&vid=".$vac);
                $dataO = $response->json();
                if(isset($dataO) && isset($dataO['data'])){
                    $list = $dataO['data']['days'];
                    $dataArr = array_filter($list, function ($where)  {
                        return $where['dateState'] == 1;
                    });
                    $date = Arr::pluck($dataArr,'dateStr');
                    $datasO = array_merge($datasO,$date);
                }
                echo "old:\n";
                echo implode(',',Arr::sort($datasO));
                echo "\n";
            }


        }

        return 0;
    }
}
